---
- name: generate reactor config secret
  local_action: >
    template
    src=reactor-config-secret.yml.j2
    dest="{{ osbs_generated_config_path }}/{{ osbs_namespace }}-{{ env }}-reactor-config-secret.yml"
  register: yaml_reactor_config_secret
  tags:
  - oc

- name: generate client config secret
  local_action: >
    template
    src=client-config-secret.conf.j2
    dest="{{ osbs_generated_config_path }}/{{ osbs_namespace }}-{{ env }}-client-config-secret.conf"
  register: yaml_client_config_secret
  tags:
  - oc

- name: generate reactor config maps
  template:
    src: reactor-config-map.yml.j2
    dest: "{{ osbs_openshift_home }}/{{ inventory_hostname }}-{{ osbs_namespace }}-{{ item.name }}.yml"
  with_items: "{{ osbs_reactor_config_maps | with_isolated_workers }}"
  register: yaml_reactor_config_maps
  tags:
  - oc

- name: create reactor config maps
  shell: >
    oc --namespace={{ osbs_namespace }} create configmap {{ item.item.name }} \
    --from-file='config.yaml'={{ item.dest }} --dry-run -o yaml | \
    oc --namespace={{ osbs_namespace }} replace --force -f -
  when: item.changed
  with_items: "{{ yaml_reactor_config_maps.results }}"
  tags:
  - oc

# Setup imagestream
- name: copy imagestream
  template:
    src: osbs-buildroot-imagestream.yml.j2
    dest: "{{ osbs_openshift_home }}/{{ inventory_hostname }}-{{ osbs_namespace }}-osbs-buildroot-imagestream.yml"
  environment: "{{ osbs_environment }}"
  when: osbs_buildroot_repository != '' and osbs_buildroot_imagestream != ''
  register: yaml_imagestream
  tags:
  - oc

- name: create imagestream
  command: >
    oc replace
    --namespace={{ osbs_namespace }}
    --force=true
    --filename={{ osbs_openshift_home }}/{{ inventory_hostname }}-{{ osbs_namespace }}-osbs-buildroot-imagestream.yml
  environment: "{{ osbs_environment }}"
  when: yaml_imagestream.changed
  tags:
  - oc

- name: update imagestream tag
  command: >
    oc import-image {{ osbs_buildroot_imagestream }}:{{ osbs_buildroot_imagestream_live_tag }}
    --namespace={{ osbs_namespace }}
    --from {{ osbs_buildroot_repository }}:{{ osbs_buildroot_imagestream_live_tag }}
    {{ " --insecure" if osbs_insecure_repository else ''}}
  environment: "{{ osbs_environment }}"
  when: osbs_buildroot_repository != '' and osbs_buildroot_imagestream != '' and osbs_buildroot_imagestream_live_tag != ''
  register: imagestream_tag_updated
  changed_when: ('The import completed successfully.' in imagestream_tag_updated.stdout)
  failed_when: ('The import completed successfully.' not in imagestream_tag_updated.stdout)
  tags:
  - oc
